{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	{eq name="$id" value="0"}
	<h3 class="pb-3">新增知识库</h3>
	<table class="layui-table">
		<tr>
			<td class="layui-td-gray-2">知识库名称<font>*</font></td>
			<td>
				<input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入知识库名称" lay-reqText="请输入知识库名称" class="layui-input">
			</td>
			<td class="layui-td-gray" rowspan="3">封面图<font>*</font></td>
			<td rowspan="3">
				<div class="layui-upload" style="text-align:center;">
				  <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="test1">上传封面图（尺寸：640x360）</button>
				  <div class="layui-upload-list" id="demo1" style="width: 176px; overflow: hidden;margin:5px auto 0">
					<img src="" style="max-width:100%; height:100px;" />
					<input type="hidden" name="thumb" value="" lay-verify="required" lay-reqText="请上传封面图">
				  </div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">首页显示<font>*</font></td>
			<td>
			  <input type="radio" name="is_index" value="1" title="是" checked>
			  <input type="radio" name="is_index" value="2" title="否" >
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">排序</td>
			<td>
				<input type="text" name="sort" placeholder="请输入排序，数字" value="0" autocomplete="off" class="layui-input">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">知识库描述</td>
			<td colspan="3"><textarea name="desc" placeholder="请输入描述，可空" class="layui-textarea"></textarea></td>
		</tr>
	</table>
	{else/}
		<h3 class="pb-3">编辑知识库</h3>
		<table class="layui-table">
		<tr>
			<td class="layui-td-gray-2">知识库名称<font>*</font></td>
			<td>
				<input type="text" name="title" value="{$detail.title}" lay-verify="required" autocomplete="off" placeholder="请输入知识库名称" lay-reqText="请输入知识库名称" class="layui-input">
			</td>
			<td class="layui-td-gray" rowspan="3">封面图<font>*</font></td>
			<td rowspan="3">
				<div class="layui-upload" style="text-align:center;">
				  <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="test1">上传封面图（尺寸：640x360）</button>
				  <div class="layui-upload-list" id="demo1" style="width: 176px; overflow: hidden;margin:5px auto 0">
					<img src="{$detail.thumb}" style="max-width: 100%; height:100px;" />
					<input type="hidden" name="thumb" value="{$detail.thumb}" lay-verify="required" lay-reqText="请上传封面图">
				  </div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">首页显示<font>*</font></td>
			<td>
			  <input type="radio" name="is_index" value="1" title="是" {eq name="$detail.is_index" value="1"}checked{/eq}>
			  <input type="radio" name="is_index" value="2" title="否" {eq name="$detail.is_index" value="2"}checked{/eq}>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">排序</td>
			<td>
				<input type="text" name="sort" value="{$detail.sort}" placeholder="请输入排序，数字" value="0" autocomplete="off" class="layui-input">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">知识库描述</td>
			<td colspan="3"><textarea name="desc" placeholder="请输入描述，可空" class="layui-textarea">{$detail.desc}</textarea></td>
		</tr>
	</table>
	{/eq}
	<div style="padding: 10px 0">
		<input type="hidden" name="id" value="{$id}">
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool'];
	function gouguInit() {
		var upload = layui.upload,tool = layui.tool, form = layui.form;
		//封面上传
		var uploadInst = upload.render({
			elem: '#test1'
			, url: '/admin/api/upload'
			, done: function (e) {
				//如果上传失败
				if (e.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				$('#demo1 input').attr('value', e.data.filepath);
				$('#demo1 img').attr('src', e.data.filepath);
			}
		});
		  
		//监听提交
		form.on('submit(webform)', function(data){
			let callback = function (e) {
				layer.msg(e.msg);
				if (e.code == 0) {
					parent.layui.tool.close(1000);
				}
			}
			tool.post("/admin/book/add", data.field, callback);
			return false;
		});
	}
	</script>
{/block}
<!-- /脚本 -->